package com.example.demo.service.Impl;

import com.example.demo.ao.DataAnalyseAo;
import com.example.demo.mapper.DataAnalyseMapper;
import com.example.demo.model.DataAnylyseDao;
import com.example.demo.service.DataAnalyseService;
import com.example.demo.vo.DataAnalyseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DataAnalyseServiceImpl implements DataAnalyseService {

    @Autowired
    private DataAnalyseMapper dataAnalyseMapper;

    @Override
    public Map<String, List<DataAnalyseVo>> getDataAnalyseInfo(DataAnalyseAo dataAnalyseAo) {

        List<DataAnalyseVo> info = dataAnalyseMapper.getDataAnalyseInfo(dataAnalyseAo);
        DataAnalyseVo dataAnalyseVo = new DataAnalyseVo();
        for (DataAnalyseVo analyseVo : info) {
            if (dataAnalyseAo.getQtyChargYm().equals(dataAnalyseVo.getQtyChargYm())) {
                dataAnalyseVo.setValue1(analyseVo.getValue1());
            }
        }


        DataAnylyseDao daoInfo = new DataAnylyseDao();
        Map<String, List<DataAnalyseVo>> map = info.stream().collect(Collectors.groupingBy(DataAnalyseVo::getType));
        map.forEach((type, list) -> {
            switch (type) {
                case "key1":
                    daoInfo.setKey1(list);
                    break;

                case "key2":
                    daoInfo.setKey2(list);
                    break;

                case "key3":
                    daoInfo.setKey3(list);
                    break;

                case "key4":
                    daoInfo.setKey4(list);
                    break;

                case "key5":
                    daoInfo.setKey5(list);

                case "key6":
                    daoInfo.setKey6(list);
            }
        });
        return map;
    }

    /**
     * 修改
     *
     * @param dataAnalyseAo
     * @return
     */
    @Override
    public int getDataAnalyseCount(List<DataAnalyseAo> dataAnalyseAo) {
        return dataAnalyseMapper.updateByMgtOrgCode(dataAnalyseAo);
    }


    @Override
    public List<DataAnalyseVo> getInfo(List<DataAnalyseAo> dataAnalyseAo) {
        List<DataAnalyseVo> info = dataAnalyseMapper.getInfo(dataAnalyseAo);
        for (DataAnalyseVo dataAnalyseVo : info) {
            for (DataAnalyseAo analyseAo : dataAnalyseAo) {
                if (analyseAo.getMgtOrgName().equals(dataAnalyseVo.getMgtOrgName())) {
                    dataAnalyseVo.setMgtOrgCode(analyseAo.getMgtOrgCode());
                }
            }
        }
        return info;
    }

    @Override
    public int updateCode(List<DataAnalyseAo> dataAnalyseAo) {
        for (DataAnalyseAo analyseAo : dataAnalyseAo) {
            dataAnalyseMapper.updateCode(analyseAo);
        }
        return 0;
    }


    @Override
    public List<DataAnalyseVo> getDataAnalyseTotal(DataAnalyseAo dataAnalyseAo) {
        // 从数据库或其他地方获取数据
        List<DataAnalyseVo> info = dataAnalyseMapper.getDataAnalyseInfo(dataAnalyseAo);

        // 创建一个新的DataAnalyseVo对象来存储合计数据
        DataAnalyseVo totalDataAnalyseVo = DataAnalyseVo.initVo();

        // 遍历数据列表，合计数据
        for (DataAnalyseVo analyseVo : info) {
            // 检查时间是否在所选时间之前或等于所选时间
            if (isBeforeOrEqual(analyseVo.getQtyChargYm(), dataAnalyseAo.getQtyChargYm())) {
               }
        }

        // 将合计后的数据添加到列表中并返回
        List<DataAnalyseVo> result = new ArrayList<>();
        result.add(totalDataAnalyseVo);
        return result;
    }

    private boolean isBeforeOrEqual(String date1, String date2) {
        // 实现一个方法来比较两个日期字符串，判断date1是否在date2之前或等于date2
        // 这里需要根据你的日期格式来实现比较逻辑
        // 例如，如果日期格式是"yyyy-MM"，你可以这样比较：
        int compareResult = date1.compareTo(date2);
        return compareResult <= 0;
    }


    public List<DataAnalyseVo> preSent(DataAnalyseAo dataAnalyseAo) {
        double a = 0;
        int b = 0;
        List<DataAnalyseVo> info = dataAnalyseMapper.getDataAnalyseInfo(dataAnalyseAo);
        for (DataAnalyseVo dataAnalyseVo : info) {
            b = Integer.parseInt(dataAnalyseVo.getValue1().subtract(dataAnalyseVo.getValue2()).toString());
        }
        return info;
    }

}
